Repository navigation
agx: never spill to scratch in the remat-only pass, drop the preamble retry - #1102
Conversation
… retry The occupancy rematerialization pass (agx_spill() with remat_only) only runs when the program already fits the full register file. It lowers pressure towards a smaller, estimated budget by recreating constants at their uses. When rematerialization alone could not reach that estimate, it moved values it cannot rematerialize to scratch. Register allocation then finished under the full budget but kept those scratch spills, and G17G-family (AGX3C) fragment preambles were recompiled from a NIR clone without rematerialization to get rid of them (ASAHI_M5_CPROG_REMAT_RETRY, on by default). Make the remat-only pass pressure-aware instead: it never moves a value it cannot rematerialize out of the register file. limit() and the W selection at block entries and loop headers keep such values even when that exceeds the estimate, and a phi whose missing sources are constants stays in a register (the constants are rematerialized along the edge). Every rematerialized value is recreated inside the live range of the value it replaces, so the result never needs more registers than the original program. agx_ra() measures it and allocates under the full file when the estimate was missed, which it did only for G18G; every chip now takes that one path, keeping the no-scratch refusal and the ra_failed exits of the full-budget spill. The remat-only pass may exceed its estimate, so the nW <= k asserts in min_algorithm() and the two W-entry computations are relaxed for it, but an explicit nW <= AGX_NUM_REGS bound stays so a broken assumption fails the assert instead of overrunning W_entry/W_exit. remat_fallback_spilled is now a backstop for memory values that other paths create. This is generic spiller logic with no hardware dependence. Programs whose remat-only pass met its budget are unchanged. The preamble recompile, its NIR clone, agx_ra_without_remat() and the ASAHI_M5_CPROG_REMAT_RETRY knob are removed. Tests: both remat-only spill tests also run on a G14G (AGX2) and a G18G (AGX3D) key, where production reaches this pass; with keep_in_registers() disabled every case fails on all keys. In NoScratchSpillFailsClosed the remat-only case now checks that the pass emits no memory value at all on a no-scratch shader (it used to expect the spill and the refusal), and the direct over-budget case runs agx_ra(). x86 agx_tests: 1617 passed, 1 skipped, 0 failed. Builds on aa2d33a by DjDeveloperr. Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
DjDeveloperr
left a comment
There was a problem hiding this comment.
Audited full diff across agx_compile.c, agx_compiler.h, agx_register_allocate.c, agx_spill.c, and test-spill-fallback.cpp: clean architectural simplification that removes the AGX3C-only preamble NIR-clone retry (ASAHI_M5_CPROG_REMAT_RETRY) and agx_ra_without_remat(), keeps non-rematerializable values in registers during the remat-only pass via keep_in_registers() across limit()/compute_w_entry_loop_header()/compute_w_entry(), and uniformly measures post-remat demand in agx_ra() across all encodings without a G18G-only branch. Covered by updated SpillFallback unit tests across AGX2/AGX3B/AGX3C/AGX3D.
|
|
What
The remat-only spill pass never moves a value it cannot rematerialize out of the register file.
agx_ra()measures the result and finishes under the full register file when the estimate is missed, on every chip. The AGX3C preamble NIR-clone retry,agx_ra_without_remat()and theASAHI_M5_CPROG_REMAT_RETRYknob go. Replaces #547, rebuilt as one signed commit on current aurora-wip (b257b3b0148).Tested: x86
agx_tests1617 passed, 1 skipped, 0 failed (aurora-mesa-xbuild container, debugoptimized). Hardware rows from before the rebase are in the details. A fresh isolated AGX2 row at this head is still owed.Risk: medium. Generic spiller and register-allocation change. Since #603 it only changes code generation on AGX2 (G13, G14) and AGX3D (G18G), where the wave-saving remat runs, and only for shaders whose remat-only pass used to spill to scratch.
Not yet
gpubench-vk, with 0 AVC, 0 failed units and 0 new kernel errors, ideally on a shader that reaches the remat-only path. Requested as a batched lab row.Review items from #547 (Dj, at ee7ec1a)
!ctx->ra_mem_exhaustedin thectx->ra_failedretry, keptagx_ra_find_regs_simple(),agx_ra_mem_bound()andagx_licm_demand_bound(), and changed theagx_compile_function_nir_attempt()comment inagx_ra_mem_bound()toagx_compile_function_nir(). The unified full-budget fallback inagx_ra()keeps the newer no-scratch refusal (!has_scratchsetsra_failed) and thera_failedexits after eachagx_spill(). It replaces the G18G-only branch and the other-chip branch.Also changed: aurora-wip's newer
SpillFallback.NoScratchSpillFailsClosedexpected the remat-only pass to spill phis to memory on a no-scratch shader and then refuse. With this change that spill cannot happen, so case 1 now checks the stronger property: the pass emits no memory value at all and does not fail. Case 2 (direct over-budget allocation on a no-scratch helper) runsagx_ra()and still fails closed with 0 memory ops.Earlier description and rows from #547
Problem
For AGX3C fragment preambles the compiler compiled the preamble on a NIR clone and, if the remat-only spill pass had spilled to scratch, threw the result away and compiled again without remat (
ASAHI_M5_CPROG_REMAT_RETRY, on by default). The retry hid the real issue: when recreating constants at their uses was not enough to reach the estimated register budget, the remat-only pass moved values it cannot recreate out to scratch.Change
The remat-only pass never moves a value it cannot recreate out of registers. It keeps those values even past the estimate, and phis whose missing sources are constants stay in registers. Every recreated constant sits inside the live range of the value it replaces, so the result never needs more registers than the original program;
agx_ra()measures the result and uses the full register file when the estimate was missed (this was already the G18G path; every chip now shares it). Removed: the NIR-clone retry wrapper,agx_ra_without_remat(), thedisable_remat/retry_without_rematparameters and theASAHI_M5_CPROG_REMAT_RETRYknob.Output only changes for shaders where the remat-only pass used to emit a scratch store; those lose that scratch traffic.
Since #603 (merged into this branch at d220af7)
#603 (Dynamic Caching) runs the wave-saving rematerialization only where the register file is split statically (
agx_has_static_register_split(): AGX2 and AGX3D). On the merged tree:ssbo.phys.layout.random.scalar.13change described below happened before the merge and cannot happen any more.agx_ra()with agx: AGX3 Dynamic Caching: last uses in divergent control flow, no static-split register budget #603's static-split condition on the remat.ee7ec1ae946runs both remat-only unit tests on G14G (AGX2) and G18G (AGX3D) keys as well. Withkeep_in_registers()disabled, every case fails on all three keys.Shim corpus on the merged head (
ee7ec1ae946tree,AGX_MESA_DEBUG=shaderdb) against aurora-wipee3abd081e4: 0 changed shaders on every chip, with identical spill and scratch totals.The corpus is all of
dEQP-VK.ssbo.*plus every second case of glsl loops/operator/functions/conditionals/switch/indexing, graphicsfuzz and compute.pipeline. On G18G only the glsl + graphicsfuzz part runs on the shim. The ssbo/compute cases hit an existingnir_lower_explicit_ioassert on both arms (the SSBO address format that #561 fixes), and cooperative_matrix cases stall the G18G shim. x86 clang + drm-shimasahi+polysuites on the merged head: 223/223.Testing (before the #603 merge)
Affects every chip, but output only changes for shaders whose remat-only pass used to spill to scratch. Rows:
Hardware rows used one combined build of all sweep branches (
sweep/all5= aurora-wip c55152d + this and the sibling sweep branches) against aurora-wip c55152d, both gcc release builds with identical meson options, loaded as user-space overlays (VK_DRIVER_FILES / LD_LIBRARY_PATH) on the production kernels; nothing installed, no reboot.Real shaders that reach the changed path. Compiled on the drm-shim with
AGX_MESA_DEBUG=shaderdb, base aurora-wip1105ea3a6e5vs this branch (a723d14606e). In 12,162dEQP-VK.ssbo.*cases the remat-only pass runs 568 times on G17S. Exactly one shader in that set,ssbo.phys.layout.random.scalar.13, used to spill, and it is the only shader whose output changes on any chip:Register count (111) and threads (896) are the same in both builds, so occupancy is unchanged.
On the M4 Pro (J616s, G17P, 7.3.0-aurora-fleet78), the same case compiled on the device gives the same stats, 2:2 / 8 B before and 0:0 / none after, and passes 3/3 on both builds. All 100 cases of
ssbo.phys.layout.random.scalar.*pass on both.A fragment-heavy corpus (87,014 cases: glsl, ubo, graphicsfuzz, spirv_assembly graphics, texture) on G17S ran the remat-only pass only twice, with no scratch spill and no preamble retry. So the removed AGX3C preamble retry never fired on these shaders. On 11,942 of those cases (13,193 fragment shaders, 11,201 AGX3 preambles) neither build changes anything or spills.
Still to run: G18G (M6) needs a hardware row, because AGX3D is where the remat-only pass still runs, and we have no M6 in our lab. The AGX2 rows above (M1 Pro, M2) were taken before the merge. #603 does not change the AGX2 path, and the shim corpus shows no change there on the merged head. A merged-head M1 Pro + M2 no-change row is queued. The M5 (G17G) row requested earlier is no longer needed: on the merged tree this PR cannot change M5 output.
Method
--warmup-ms 300 --runs 5 --run-ms 250, arms interleaved, 70 C cool-down gate.Offline check
3031 offline compiles (libagx for every precompiled target plus every unit-test compile) produced bit-identical binaries, register counts and scratch sizes against aurora-wip. Only 3 of them ran the remat pass, so this check is thin on that path; the hardware rows above cover it.
New/changed unit tests in
test-spill-fallback.cpp:RematOnlyKeepsPhiWebsInRegisters(no scratch at all) andRematOnlyRematerializesConstantsWithoutScratch(G17G fragment preamble, 32 halves against a 24-half budget).Corpus method
ASAHI_SHIM_CHIP=g17s-kernel|g17p|g14g|g13x; submits are no-ops, compiles are real).AGX_MESA_DEBUG=shaderdblines compared between builds. Remat-only pass and spill counts came from a separate base build with a stderr print inagx_ra(), which is not part of this PR.a723d14606e: the three remat-only invariant asserts now also boundnWbyAGX_NUM_REGS, soW_entryandW_exitcannot overrun. Theremat_fallback_spilledandinsert_spill()comments describe the new behaviour. x86 asahi suite: everything passes excepthk_dual_source_gpl, which fails the same way on the base (a clang-only failure fixed on aurora-wip by asahi: keep the merged PPP scratch words 4-byte aligned #562).Builds on aa2d33a.